home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / mt / mt.c < prev    next >
C/C++ Source or Header  |  1992-05-18  |  7KB  |  269 lines

  1. /*
  2.  * mt.c --
  3.  *    Manipulate a tape drive.
  4.  *      This is a bastardized version of Sprite's native tape
  5.  *      utility hacked to understand standard mt command options.
  6.  */
  7. #include <sprite.h>
  8. #include <status.h>
  9. #include <time.h>
  10. #include <sys/file.h>
  11. #include <fs.h>
  12. #include <dev/tape.h>
  13. #include <stdio.h>
  14. #include <option.h>
  15. #include <errno.h>
  16. #include <limits.h>
  17. #include <stdlib.h>
  18. #include "mtio.h"
  19.  
  20. static int CmdLookup();
  21. static int CvtInteger();
  22.  
  23. #define DEFTAPE "/dev/rmt12";
  24. char *tapeFile = "";
  25.  
  26. #define NUMCMDS  17
  27. typedef struct Cmd {
  28.     char *name;
  29.     int id;
  30. } Cmd;
  31.  
  32. Option optionArray[] = {
  33.     { OPT_STRING, "f", (Address)&tapeFile, "Name of tape device" }, 
  34. };
  35. int numOptions = sizeof(optionArray) / sizeof(Option);
  36.  
  37. main(argc, argv)
  38. int argc;
  39. char **argv;
  40. {
  41.     int oldOffset;
  42.     ReturnStatus status;
  43.     int tapeStream;
  44.     int count = 1;
  45.     int cmdIndx;
  46.     int openFlags;
  47.     Dev_TapeStatus tapeStatus;
  48.     char *defTape = DEFTAPE;
  49.  
  50.     argc = Opt_Parse(argc, argv, optionArray, numOptions, 0);
  51.  
  52.     if (!*tapeFile) {
  53.     if ((tapeFile=getenv("TAPE")) == (char *)NULL) {
  54.         tapeFile = defTape;
  55.     }
  56.     }
  57.  
  58.     if ((argc < 2) || (argc > 3)) {
  59.         fprintf(stderr,"usage: mt [ -f device ] command [ count ]\n");
  60.     exit(-1);
  61.     }
  62.  
  63.     if ((cmdIndx=CmdLookup(argv[1])) < 0) {
  64.         fprintf(stderr,"mt: unknown command: %s\n", argv[1]);
  65.     exit(-1);
  66.     }
  67.  
  68.     if ((argc > 2) && (CvtInteger(argv[2],1,INT_MAX,&count) != SUCCESS)) {
  69.         fprintf(stderr,"mt: bad count: %s\n", argv[2]);
  70.     exit(-1);
  71.     }
  72.  
  73.     if (cmdIndx == MTWEOF) {
  74.     openFlags = O_RDWR;
  75.     } else {
  76.     openFlags = O_RDONLY;
  77.     }
  78.     tapeStream = open(tapeFile, openFlags, 0);
  79.     if (tapeStream < 0) {
  80.     perror("Can't open tape drive");
  81.     exit(errno);
  82.     }
  83.  
  84.     switch (cmdIndx) {
  85.     case MTWEOF:
  86.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_WEOF, count);
  87.     if (status != SUCCESS) {
  88.         Stat_PrintMsg(status, "Can't write file mark(s)");
  89.         exit(status);
  90.     }
  91.     break;
  92.     case MTFSF:
  93.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_SKIP_FILES, count);
  94.     if (status != SUCCESS) {
  95.         Stat_PrintMsg(status, "Can't skip tape files forward");
  96.         exit(status);
  97.     }
  98.     break;
  99.     case MTBSF:
  100.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_SKIP_FILES, -count);
  101.     if (status != SUCCESS) {
  102.         Stat_PrintMsg(status, "Can't skip tape files backward");
  103.         exit(status);
  104.     }
  105.     break;
  106.     case MTFSR:
  107.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_SKIP_BLOCKS, count);
  108.     if (status != SUCCESS) {
  109.         Stat_PrintMsg(status, "Can't skip tape records forward");
  110.         exit(status);
  111.     }
  112.     break;
  113.     case MTBSR:
  114.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_SKIP_BLOCKS, -count);
  115.     if (status != SUCCESS) {
  116.         Stat_PrintMsg(status, "Can't skip tape records backward");
  117.         exit(status);
  118.     }
  119.     break;
  120.     case MTREW:
  121.     status = Ioc_Reposition(tapeStream, IOC_BASE_ZERO, 0, &oldOffset);
  122.     if (status != SUCCESS) {
  123.         Stat_PrintMsg(status, "Can't rewind tape drive");
  124.         exit(status);
  125.     }
  126.     break;
  127.     case MTOFFL:
  128.     status = Ioc_Reposition(tapeStream, IOC_BASE_ZERO, 0, &oldOffset);
  129.     if (status != SUCCESS) {
  130.         Stat_PrintMsg(status, "Can't rewind tape drive");
  131.         exit(status);
  132.     }
  133.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_UNLOAD, 0);
  134.     if (status != SUCCESS) {
  135.         Stat_PrintMsg(status, "Can't unload tape");
  136.         exit(status);
  137.     }
  138.     break;
  139.     case MTASF:
  140.     status = Ioc_Reposition(tapeStream, IOC_BASE_ZERO, 0, &oldOffset);
  141.     if (status != SUCCESS) {
  142.         Stat_PrintMsg(status, "Can't rewind tape drive");
  143.         exit(status);
  144.     }
  145.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_SKIP_FILES, count);
  146.     if (status != SUCCESS) {
  147.         Stat_PrintMsg(status, "Can't skip tape files forward");
  148.         exit(status);
  149.     }
  150.     break;
  151.     case MTRETEN:
  152.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_RETENSION, 1);
  153.     if (status != SUCCESS) {
  154.         Stat_PrintMsg(status, "Can't retension tape");
  155.         exit(status);
  156.     }
  157.     break;
  158.     case MTERASE:
  159.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_ERASE, 0);
  160.     if (status != SUCCESS) {
  161.         Stat_PrintMsg(status, "Can't erase tape");
  162.         exit(status);
  163.     }
  164.     break;
  165.     case MTEOM:
  166.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_SKIP_EOD, 0);
  167.     if (status != SUCCESS) {
  168.         Stat_PrintMsg(status, "Can't position at end-of-tape");
  169.         exit(status);
  170.     }
  171.     break;
  172.     case MTNBSF:
  173.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_SKIP_FILES, -(count+1));
  174.     if (status != SUCCESS) {
  175.         Stat_PrintMsg(status, "Can't skip tape files backward");
  176.         exit(status);
  177.     }
  178.     status = Ioc_TapeCommand(tapeStream, IOC_TAPE_SKIP_FILES, 1);
  179.     if (status != SUCCESS) {
  180.         Stat_PrintMsg(status, "Can't skip tape files foreward");
  181.         exit(status);
  182.     }
  183.     break;
  184.     case MTCACHE:
  185.     fprintf(stderr,"Sorry the cache option is not supported.\n");
  186.     break;
  187.     case MTNOCACHE:
  188.     fprintf(stderr,"Sorry the nocache option is not supported.\n");
  189.     break;
  190.     case MTSTATUS:
  191.     bzero((char *) &tapeStatus, sizeof(tapeStatus));
  192.     status = Fs_IOControl(tapeStream, IOC_TAPE_STATUS, 0, NULL, 
  193.         sizeof(tapeStatus), &tapeStatus);
  194.     if (status != SUCCESS) {
  195.         Stat_PrintMsg(status, "Can't get status");
  196.         exit(status);
  197.     }
  198.     printf("Type : 0x%x\n", tapeStatus.type);
  199.     printf("Serial : %s\n", tapeStatus.serial);
  200.     printf("Block size : %d\n", tapeStatus.blockSize);
  201.     printf("Current block : %d\n", tapeStatus.position);
  202.     printf("Remaining blocks : %d\n", tapeStatus.remaining);
  203.     printf("Data errors : %d\n", tapeStatus.dataError);
  204.     printf("Read/Write retry : %d\n", tapeStatus.readWriteRetry);
  205.     printf("Tracking retry : %d\n", tapeStatus.trackingRetry);
  206.     printf("Write protect : %d\n", tapeStatus.writeProtect);
  207.     printf("Buffered mode : %d\n", tapeStatus.bufferedMode);
  208.     printf("Speed : %d\n", tapeStatus.speed);
  209.     printf("Density : %d\n", tapeStatus.density);
  210.     break;
  211.     }
  212.  
  213.     close(tapeStream);
  214.     exit(SUCCESS);
  215. }
  216.  
  217. static int
  218. CmdLookup(string)
  219.     char *string;
  220. {
  221.     int i;
  222.     static Cmd cmds[NUMCMDS] = {
  223.     "asf", MTASF,
  224.     "bsf", MTBSF,
  225.     "bsr", MTBSR,
  226.     "cache", MTCACHE,
  227.     "eof", MTWEOF,
  228.     "eom", MTEOM,
  229.     "erase", MTERASE,
  230.     "fsf", MTFSF,
  231.     "fsr", MTFSR,
  232.     "nbsf", MTNBSF,
  233.     "nocache", MTNOCACHE,
  234.     "offline", MTOFFL,
  235.     "retension", MTRETEN,
  236.     "rewind", MTREW,
  237.     "rewoffl", MTOFFL,
  238.     "status", MTSTATUS,
  239.     "weof", MTWEOF
  240.     };
  241.  
  242.     for (i=0; i<NUMCMDS; i++) {
  243.     if (strcmp(string,cmds[i].name) == 0) {
  244.         return cmds[i].id;
  245.     }
  246.     }
  247.  
  248.     return -1;
  249. }
  250.  
  251. static int
  252. CvtInteger(string, low, high, valPtr)
  253.     char *string;             /* ascii number to convert */
  254.     int low;                  /* lowest acceptable value */
  255.     int high;                 /* highest acceptable value */
  256.     int *valPtr;              /* converted value */
  257. {
  258.     char *endPtr;
  259.  
  260.     *valPtr = strtol(string, &endPtr, 0);
  261.  
  262.     if ((*endPtr != '\0') || (*valPtr < low) || (*valPtr > high)) {
  263.     return FAILURE;
  264.     }
  265.  
  266.     return SUCCESS;
  267.     
  268. }
  269.